import request from '../../utils/request'

/**
 * 活动相关API接口
 */

export interface Activity {
  id: string
  title: string
  posterUrl: string
  category: string
  startTime: string
  endTime: string
  location: string
  maxParticipants: number
  signupCount: number
  description: string
  tags: string[]
  price: number
  contactPhone: string
  status: 'upcoming' | 'registering' | 'ongoing' | 'completed' | 'canceled'
  createTime: string
}

export interface ActivityListParams {
  keyword?: string
  category?: string
  status?: string
  page?: number
  pageSize?: number
}

export interface ActivityFormData {
  id?: string
  title: string
  posterUrl: string
  category: string
  startTime: string
  endTime: string
  location: string
  maxParticipants: number
  description: string
  tags: string[]
  price: number
  contactPhone: string
  status: 'upcoming' | 'registering' | 'ongoing' | 'completed' | 'canceled'
}

export interface SignupInfo {
  id: string
  activityId: string
  userId: string
  name: string
  gender: 'male' | 'female'
  phone: string
  idCard?: string
  signupTime: string
  payStatus: 'paid' | 'unpaid'
  remark?: string
}

/**
 * 获取活动列表
 */
export function getActivities(params: ActivityListParams = {}) {
  return request<Activity[]>({
    url: '/activities',
    method: 'GET',
    params
  })
}

/**
 * 获取活动详情
 */
export function getActivityDetail(id: string) {
  return request<Activity>({
    url: `/activities/${id}`,
    method: 'GET'
  })
}

/**
 * 创建活动
 */
export function createActivity(data: ActivityFormData) {
  return request<Activity>({
    url: '/activities',
    method: 'POST',
    data
  })
}

/**
 * 更新活动
 */
export function updateActivity(id: string, data: Partial<ActivityFormData>) {
  return request<Activity>({
    url: `/activities/${id}`,
    method: 'PUT',
    data
  })
}

/**
 * 删除活动
 */
export function deleteActivity(id: string) {
  return request<null>({
    url: `/activities/${id}`,
    method: 'DELETE'
  })
}

/**
 * 更新活动状态
 */
export function updateActivityStatus(id: string, status: Activity['status']) {
  return request<Activity>({
    url: `/activities/${id}/status`,
    method: 'PATCH',
    data: { status }
  })
}

/**
 * 取消活动
 */
export function cancelActivity(id: string) {
  return request<Activity>({
    url: `/activities/${id}/cancel`,
    method: 'PATCH'
  })
}

/**
 * 获取活动报名列表
 */
export function getActivitySignups(activityId: string) {
  return request<SignupInfo[]>({
    url: `/activities/${activityId}/signups`,
    method: 'GET'
  })
}

/**
 * 取消报名
 */
export function cancelSignup(activityId: string, signupId: string) {
  return request<null>({
    url: `/activities/${activityId}/signups/${signupId}/cancel`,
    method: 'PATCH'
  })
}

/**
 * 上传活动海报
 */
export function uploadActivityPoster(data: FormData) {
  return request<{ url: string }>({
    url: '/upload/activity',
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data
  })
} 